//
//  BNDApiManager.m
//  OfficeDoc
//
//  Created by pie on 2022/4/5.
//  Copyright © 2022 pie. All rights reserved.
//

#import "BNDApiManager.h"
#import "NSObject+YYModel.h"
#import "BNDClassifyModel.h"
#import "BNDOfficeDocModel.h"
#import "BndUser.h"
#import "BNDFileInfo.h"
#import "BNDOfficeDocKit.h"

//分类数据
NSString *const BNDOfficeDocClassificationList        = @"AnnouncementClassificationController/list/page";
//授权处理
NSString *const BNDOfficeDocCInitCompanyAdmin         = @"auth/initCompanyAdmin";
//公告列表
NSString *const BNDOfficeDocCAnnouncementController   = @"AnnouncementController/list/page";
//公文详情列表
NSString *const BNDOfficeDocCArchiveController        = @"ArchiveController";
//搜索接口
NSString *const BNDOfficeDocCSearchrr                 = @"AnnouncementController/announcement/search";
//首页卡片列表
NSString *const BNDOfficeDocCAnnouncementCard         = @"AnnouncementController/get/announcement/card";
//首页弹窗接口
NSString *const BNDOfficeDocPopWindos                 = @"AnnouncementController/lasted/popup/windows";
//首页弹窗关闭
NSString *const BNDOfficeDocPopWindosOff                = @"AnnouncementUserRecordController/popup/off";

//请求用户信息
NSString *const BNDOfficeDocListUserInfoVoByIds               = @"userInfo/listUserInfoVoByIds";
//请求公司信息
NSString *const BNDOfficeDocFindCompanyInfoById               = @"companyInfo/findCompanyInfoById";
//角色信息
NSString *const BNDOfficeDocUserInfo              = @"auth/userInfo";
//更新阅读状态
NSString *const BNDOfficeDocRead              = @"AnnouncementUserRecordController/read";
//上传信息配置
NSString *const BNDOfficeDocFileUrl            = @"StsTokenController/getToken";
//管理员可以看到签到统计
NSString *const BNDOfficeDocStatistics            = @"AnnouncementController/announcement/statistics";
//阅读详情
NSString *const BNDOfficeDocReadDtails           = @"AnnouncementUserRecordController/read/details";
//签署功能
NSString *const BNDOfficeDocAnnouncementSign           = @"AnnouncementUserRecordController/announcement/sign";



@implementation BNDApiManager

+ (LKRequestManager *)officeDocRequestClassList:(void (^)(NSArray<BNDClassifyModel*>*moreClass))success
                                        failure:(LKJsonQueryFailureBlock)failure
{
    NSDictionary *param = @{
        @"companyOpenId":@"5",
        @"openId":@"1",
        
    };
    
    LKRequestManager *requestManager = [LKRequestManager requestManager:SERVER_URL];
    [requestManager getJSONWithRelativeUrl:BNDOfficeDocClassificationList parameters:param success:^(id  _Nonnull responseObject) {
        NSArray <BNDClassifyModel *> *moreClass = [NSArray modelArrayWithClass:BNDClassifyModel.class json:responseObject[@"data"][@"records"]];
        NSMutableArray *array = [NSMutableArray arrayWithArray:moreClass];
        if(array == nil){
            array = [NSMutableArray array];
        }
        BNDClassifyModel *model = [BNDClassifyModel new];
        model.name = @"全部";
        model.classifyId = @"";
        [array insertObject:model atIndex:0];
        BND_BLOCK_EXEC(success,array);
    } failure:failure];
    
    return requestManager;
}

+ (LKRequestManager *)officeDocInitCompanyAdmin:(void (^)(void))success
                                        failure:(LKJsonQueryFailureBlock)failure
{
    NSDictionary *param = @{
    };
    
    LKRequestManager *requestManager = [LKRequestManager requestManager:SERVER_URL];
    [requestManager getJSONWithRelativeUrl:BNDOfficeDocCInitCompanyAdmin parameters:param success:^(id  _Nonnull responseObject) {
        BND_BLOCK_EXEC(success);
    } failure:failure];
    
    
    return requestManager;
}

+ (LKRequestManager *)officeDocAnnouncementList:(NSInteger)pageNo classID:(NSString*)classID success:(void (^)(NSArray<BNDOfficeDocModel*>*moreClass))success
                                        failure:(LKJsonQueryFailureBlock)failure
{
    NSDictionary *param = @{
        @"classificationId":  TTStringNotNil(classID),
        @"current":  TTStringNotNil([@(pageNo) stringValue]),
        @"size":  @"20",
    };
    
    LKRequestManager *requestManager = [LKRequestManager requestManager:SERVER_URL];
    [requestManager getJSONWithRelativeUrl:BNDOfficeDocCAnnouncementController parameters:param success:^(id  _Nonnull responseObject) {
        NSArray <BNDOfficeDocModel *> *moreClass = [NSArray modelArrayWithClass:BNDOfficeDocModel.class json:responseObject[@"data"][@"records"]];
        BND_BLOCK_EXEC(success,moreClass);
    } failure:failure];
    
    return requestManager;
}

+ (LKRequestManager *)officeDocRequestArchiveController:(NSString*)modelId success:(void (^)(BNDFileInfo *info))success
                                                failure:(LKJsonQueryFailureBlock)failure
{
    NSDictionary *param = @{
    };
    
    LKRequestManager *requestManager = [LKRequestManager requestManager:SERVER_URL];
    [requestManager getJSONWithRelativeUrl:[NSString stringWithFormat:@"%@/%@",BNDOfficeDocCArchiveController,modelId]  parameters:param success:^(id  _Nonnull responseObject) {

        BNDFileInfo *model = [BNDFileInfo modelWithDictionary:responseObject[@"data"]];
        BND_BLOCK_EXEC(success,model);
    } failure:failure];
    
    return requestManager;
}

+ (LKRequestManager *)officeDocRequestSearchrr:(NSInteger)pageNo  content:(NSString*)content success:(void (^)(NSArray<BNDOfficeDocModel*>*moreClass))success
                                       failure:(LKJsonQueryFailureBlock)failure
{
    NSDictionary *param = @{
        @"content":  TTStringNotNil(content),
        @"current":  TTStringNotNil([@(pageNo) stringValue]),
        @"size":  @"20",
    };
    
    LKRequestManager *requestManager = [LKRequestManager requestManager:SERVER_URL];
    [requestManager getJSONWithRelativeUrl:BNDOfficeDocCSearchrr parameters:param success:^(id  _Nonnull responseObject) {
        NSArray <BNDOfficeDocModel *> *moreClass = [NSArray modelArrayWithClass:BNDOfficeDocModel.class json:responseObject[@"data"]];
        BND_BLOCK_EXEC(success,moreClass);
    } failure:failure];
    
    return requestManager;
}

+ (LKRequestManager *)officeDocRequestAnnouncementCard:(void (^)(NSArray<BNDOfficeDocModel*>*moreClass))success
                                               failure:(LKJsonQueryFailureBlock)failure
{
    NSDictionary *param = @{
        @"receiver":  @"0",
    };
    
    
    LKRequestManager *requestManager = [LKRequestManager requestManager:SERVER_URL];
    [requestManager getJSONWithRelativeUrl:BNDOfficeDocCAnnouncementCard parameters:param success:^(id  _Nonnull responseObject) {
        NSArray <BNDOfficeDocModel *> *moreClass = [NSArray modelArrayWithClass:BNDOfficeDocModel.class json:responseObject[@"data"]];
        BND_BLOCK_EXEC(success,moreClass);
    } failure:failure];
    
    return requestManager;
}

+ (LKRequestManager *)officeDocRequestPopWindos:(void (^)(NSArray<BNDOfficeDocModel*>*moreClass))success
                                        failure:(LKJsonQueryFailureBlock)failure
{
    NSDictionary *param = @{
    };
    
    LKRequestManager *requestManager = [LKRequestManager requestManager:SERVER_URL];
    [requestManager getJSONWithRelativeUrl:BNDOfficeDocPopWindos parameters:param success:^(id  _Nonnull responseObject) {
        NSArray <BNDOfficeDocModel *> *moreClass = [NSArray modelArrayWithClass:BNDOfficeDocModel.class json:responseObject[@"data"]];
        BND_BLOCK_EXEC(success,moreClass);
    } failure:failure];
    
    return requestManager;
}


+ (LKRequestManager *)officeDocRequestUserInfo:(void (^)(BOOL isManage))success
                                       failure:(LKJsonQueryFailureBlock)failure
{
    NSDictionary *param = @{
    };
    
    LKRequestManager *requestManager = [LKRequestManager requestManager:SERVER_URL];
    [requestManager getJSONWithRelativeUrl:BNDOfficeDocUserInfo parameters:param success:^(id  _Nonnull responseObject) {
        
        NSString *admin = responseObject[@"data"][@"roleCode"];
        if([admin isEqualToString:@"company_admin"]||[admin isEqualToString:@"sub_admin"]||[admin isEqualToString:@"doc_admin"]){
            BND_BLOCK_EXEC(success,YES);
        }else{
            BND_BLOCK_EXEC(success,NO);
        }
    } failure:failure];
    
    return requestManager;
}


+ (LKRequestManager *)officeDocStatistics:(NSString*)officDocId success:(void (^)(NSString* readCount,NSString* readHasBeenSigned,NSString* readUnsigned,NSString * unreadCount))success
                                  failure:(LKJsonQueryFailureBlock)failure
{
    NSDictionary *param = @{
    };
    LKRequestManager *requestManager = [LKRequestManager requestManager:SERVER_URL];
    [requestManager getJSONWithRelativeUrl:[NSString stringWithFormat:@"%@/%@",BNDOfficeDocStatistics,officDocId] parameters:param success:^(id  _Nonnull responseObject) {
        
        NSString* readCount = responseObject[@"data"][@"readCount"];
        NSString* readHasBeenSigned = responseObject[@"data"][@"readHasBeenSigned"];
        NSString* readUnsigned = responseObject[@"data"][@"readUnsigned"];
        NSString* unreadCount = responseObject[@"data"][@"unreadCount"];
        BND_BLOCK_EXEC(success,readCount,readHasBeenSigned,readUnsigned,unreadCount);
    } failure:failure];
    
    return requestManager;
}

+ (LKRequestManager *)officeDocRequestRead:(NSString*)officDocId success:(void (^)(void))success
                                   failure:(LKJsonQueryFailureBlock)failure
{
    
    NSDictionary *param = @{
    };
    
    LKRequestManager *requestManager = [LKRequestManager requestManager:SERVER_URL];
    [requestManager putJSONWithRelativeUrl:[NSString stringWithFormat:@"%@/%@",BNDOfficeDocRead,officDocId] parameters:param success:^(id  _Nonnull responseObject) {
        BND_BLOCK_EXEC(success);
    } failure:failure];
    
    return requestManager;
}

+ (LKRequestManager *)officeDocRequestPopWindosOff:(NSString*)officDocId success:(void (^)(void))success
                                   failure:(LKJsonQueryFailureBlock)failure
{
    
    NSDictionary *param = @{
    };
    
    LKRequestManager *requestManager = [LKRequestManager requestManager:SERVER_URL];
    [requestManager putJSONWithRelativeUrl:[NSString stringWithFormat:@"%@/%@",BNDOfficeDocPopWindosOff,officDocId] parameters:param success:^(id  _Nonnull responseObject) {
        BND_BLOCK_EXEC(success);
    } failure:failure];
    
    return requestManager;
}


+ (LKRequestManager *)officeDocRequestReadDtails:(NSInteger)pageNo  announcementId:(NSString*)announcementId type:(NSInteger)type success:(void (^)(NSArray<BndUser*>*moreClass))success
                                         failure:(LKJsonQueryFailureBlock)failure
{
    NSDictionary *param = @{
        @"announcementId":  TTStringNotNil(announcementId),
        @"type":  TTStringNotNil([@(type) stringValue]),
        @"current":  TTStringNotNil([@(pageNo) stringValue]),
        @"size":  @"20",
    };
    
    LKRequestManager *requestManager = [LKRequestManager requestManager:SERVER_URL];
    [requestManager getJSONWithRelativeUrl:BNDOfficeDocReadDtails parameters:param success:^(id  _Nonnull responseObject) {
        NSArray <BndUser *> *moreClass = [NSArray modelArrayWithClass:BndUser.class json:responseObject[@"data"]];
        BND_BLOCK_EXEC(success,moreClass);
    } failure:failure];
    
    return requestManager;
}

+ (LKJsonRequestManager *)officeDocAnnouncementSign:(NSString*)officDocId signaturePic:(NSString*)signaturePic success:(void (^)(void))success
                                            failure:(LKJsonQueryFailureBlock)failure

{
    NSDictionary *param = @{
        @"id":  TTStringNotNil(officDocId),
        @"signaturePic":  TTStringNotNil(signaturePic),
    };
    
    LKJsonRequestManager *requestManager = [LKJsonRequestManager requestManager:SERVER_URL];
    [requestManager postJSONWithRelativeUrl:[NSString stringWithFormat:@"%@?currentCompanyOpenId=%@&currentOpenId=%@",BNDOfficeDocAnnouncementSign,[BNDPreference sharedInstance].companyOpenId,[BNDPreference sharedInstance].openId] parameters:param success:^(id  _Nonnull responseObject) {
        BND_BLOCK_EXEC(success);
    } failure:failure];
    
    return requestManager;
}

+ (LKJsonRequestManager *)officeDocDetails:(NSString*)officDocId success:(void (^)(BNDOfficeDocModel *model))success
                                            failure:(LKJsonQueryFailureBlock)failure

{
    NSDictionary *param = @{

    };
    
    LKJsonRequestManager *requestManager = [LKJsonRequestManager requestManager:SERVER_URL];

    [requestManager getJSONWithRelativeUrl:[NSString stringWithFormat:@"AnnouncementController/%@",officDocId] parameters:param success:^(id  _Nonnull responseObject) {
        BNDOfficeDocModel *model = [BNDOfficeDocModel modelWithDictionary:responseObject[@"data"]];
        BND_BLOCK_EXEC(success,model);
    } failure:failure];
    
    return requestManager;
}


#pragma mark - 文件配置服务

+ (LKRequestManager *)officeDocRequestFileUrl:(void (^)(NSString* accessKeyId,NSString* securityToken,NSString* accessKeySecret,NSString * expiration))success
                                      failure:(LKJsonQueryFailureBlock)failure
{
    NSDictionary *param = @{
    };
    
    
    LKRequestManager *requestManager = [LKRequestManager requestManager:SERVER_UPLOAD_URL];
    [requestManager getJSONWithRelativeUrl:BNDOfficeDocFileUrl parameters:param success:^(id  _Nonnull responseObject) {
        NSString* accessKeyId = responseObject[@"data"][@"accessKeyId"];
        NSString* securityToken = responseObject[@"data"][@"securityToken"];
        NSString* accessKeySecret = responseObject[@"data"][@"accessKeySecret"];
        NSString* expiration = responseObject[@"data"][@"expiration"];
        
        BND_BLOCK_EXEC(success,accessKeyId,securityToken,accessKeySecret,expiration);
    } failure:failure];
    
    return requestManager;
}


#pragma mark - 用户信息接口服务

+ (LKJsonRequestManager *)officeDocRequestUserInfoVoByIds:(void (^)(NSArray<BndUser*>*moreClass))success
                                                  failure:(LKJsonQueryFailureBlock)failure
{
    
    LKJsonRequestManager *requestManager = [LKJsonRequestManager requestManager:SERVER_IN_URL];
    [requestManager postJSONWithRelativeAllUrl:BNDOfficeDocListUserInfoVoByIds parameters:@[[BNDPreference sharedInstance].openId] success:^(id  _Nonnull responseObject) {
        NSArray <BndUser *> *moreClass = [NSArray modelArrayWithClass:BndUser.class json:responseObject[@"data"]];
        BND_BLOCK_EXEC(success,moreClass);
    } failure:failure];
    
    return requestManager;
}


+ (LKRequestManager *)officeDocRequestFindCompanyInfoById:(void (^)(NSString*companyName))success
                                                  failure:(LKJsonQueryFailureBlock)failure
{
    
    NSDictionary *param = @{
    };
    
    LKRequestManager *requestManager = [LKRequestManager requestManager:SERVER_IN_URL];
    [requestManager getJSONWithRelativeUrl:BNDOfficeDocFindCompanyInfoById parameters:param success:^(id  _Nonnull responseObject) {
        NSString *companyName = responseObject[@"data"][@"companyName"];
        BND_BLOCK_EXEC(success,companyName);
    } failure:failure];
    
    return requestManager;
}





@end
